#include <iostream>
#include <vector>
#include <thread>
#include <mutex>
#include <condition_variable>

std::mutex mtx1;
std::mutex mtx;
int i = 0;
std::condition_variable cv;

void func1()
{
    for (; i < 100;)
    {
        if (i % 2 == 1)
        {
            std::unique_lock<std::mutex> lmtx(mtx);
            cv.wait(lmtx);
        }
        std::cout << "[thread " << std::this_thread::get_id() << " ]: " << i << std::endl;
        {
            std::unique_lock<std::mutex> ul(mtx1);
            i++;
        }
        cv.notify_one();
    }
}
void func2()
{
    for (; i < 100;)
    {
        if (i % 2 == 0)
        {
            std::unique_lock<std::mutex> lmtx(mtx);
            cv.wait(lmtx);
        }
        std::cout << "[thread " << std::this_thread::get_id() << " ]: " << i << std::endl;
        {
            std::unique_lock<std::mutex> ul(mtx1);
            i++;
        }
        cv.notify_one();
    }
}
int main()
{
    std::vector<std::thread> ths;
    ths.emplace_back(func1);
    ths.emplace_back(func2);

    for (auto& e : ths)
    {
        e.join();
    }


    return 0;
}